/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.multishop.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.model.NotifyTemplate;
import com.yami.shop.bean.model.NotifyTemplateShop;
import com.yami.shop.common.annotation.SysLog;

import javax.validation.Valid;

import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.NotifyTemplateService;
import com.yami.shop.service.NotifyTemplateShopService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 *
 * @author lhd
 * @date 2020-07-04 15:21:20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/multishop/notifyTemplateShop" )
public class NotifyTemplateShopController {

    private final NotifyTemplateShopService notifyTemplateShopService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param notifyTemplateShop
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<NotifyTemplateShop>> getNotifyTemplateShopPage(PageParam<NotifyTemplateShop> page, NotifyTemplateShop notifyTemplateShop) {
        return ResponseEntity.ok(notifyTemplateShopService.page(page, new LambdaQueryWrapper<NotifyTemplateShop>()));
    }


    /**
     * 通过id查询
     * @param notifyShopId id
     * @return 单个数据
     */
    @GetMapping("/info/{notifyShopId}" )
    public ResponseEntity<NotifyTemplateShop> getById(@PathVariable("notifyShopId") Long notifyShopId) {
        return ResponseEntity.ok(notifyTemplateShopService.getById(notifyShopId));
    }

    /**
     * 增加关联
     * @return 是否增加关联成功
     */
    @SysLog("增加关联" )
    @PutMapping
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid NotifyTemplate notifyTemplate) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<NotifyTemplateShop> notifyTemplateShops = notifyTemplateShopService.list(new LambdaQueryWrapper<NotifyTemplateShop>()
                                    .eq(NotifyTemplateShop::getShopId, shopId).eq(NotifyTemplateShop::getTemplateId,notifyTemplate.getTemplateId()));
        if(CollectionUtils.isNotEmpty(notifyTemplateShops)){
            notifyTemplateShopService.remove(new LambdaQueryWrapper<NotifyTemplateShop>()
                    .eq(NotifyTemplateShop::getShopId, shopId).eq(NotifyTemplateShop::getTemplateId,notifyTemplate.getTemplateId()));
        }
        NotifyTemplateShop notifyTemplateShop = new NotifyTemplateShop();
        notifyTemplateShop.setShopId(shopId);
        notifyTemplateShop.setTemplateId(notifyTemplate.getTemplateId());
        notifyTemplateShop.setNotifyTypes(arrayChangeList(notifyTemplate.getTemplateTypeList()));
        return ResponseEntity.ok(notifyTemplateShopService.save(notifyTemplateShop));
    }

    private String arrayChangeList(List<Integer> templateTypeList) {
        StringBuilder templateTypes = new StringBuilder();
        for (Integer templateType : templateTypeList) {
            templateTypes.append(templateType);
            templateTypes.append(StrUtil.COMMA);
        }
        templateTypes.deleteCharAt(templateTypes.length() - 1);
        return templateTypes.toString();
    }

}
